package com.ldl.origami.origamiService;

import com.ldl.origami.dao.NewsMapper;
import com.ldl.origami.domain.News;
import com.ldl.origami.domain.NewsExample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 功能描述:
 * 作者: LDL
 * 创建时间: 2014/8/26 15:56
 */
@Service
public class NewsService {


    static Logger logger = LoggerFactory.getLogger(NewsService.class);

    @Autowired(required = false)
    private NewsMapper newsMapper;
    /**
     * 添加消息
     *
     * @param news
     * @return
     */
    public void addNewsWithUnAudit(News news) {

        try {
            newsMapper.insert(news);
        } catch (Exception e) {
            logger.debug("添加消息: "+e.getMessage());
        }
    }

    /**
     * 返回用户的未读消息
     *
     * @param userName
     * @return
     */
    public int getUnReadNews(String userName) {
        int count = 0;
        NewsExample example = new NewsExample();
        example.createCriteria().andUserNameEqualTo(userName).andStateEqualTo(0);
        example.or().andUserNameEqualTo("ALL").andStateEqualTo(0);
        List<News> news = null;
        try{
            news = newsMapper.selectByExample(example);
        }catch (Exception e){
            logger.debug("获取用户消息: " + e.getMessage());
            return 0;
        }
        if(news != null){
            count = news.size();
        }
        return count;
    }
}


